
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

from config import read_config
import ast

class Sever_email():

    def __init__(self, path, content):
        self.path = path
        self.content = content
        self.msg = MIMEMultipart()
        self.msg['Subject'] = read_config.congfig_read(self.path).get('email', 'Subject')
        self.msg['From'] = read_config.congfig_read(self.path).get('email', 'SendName')
        self.msg['To'] = ",".join(ast.literal_eval(read_config.congfig_read(self.path).get('email', 'receive'))) # Join the list into a string
        text1 = MIMEText(self.content)
        self.msg.attach(text1)

    def email_fujian(self):
        text2 = MIMEApplication(open(read_config.congfig_read(self.path).get('email', 'file_path'), 'rb').read())
        text2.add_header('Content-Disposition', 'attachment', filename='wangjiaxing.png')
        self.msg.attach(text2)

    def smtp_sever(self):
        sever = smtplib.SMTP(read_config.congfig_read(self.path).get('email', 'smtp'),
                            read_config.congfig_read(self.path).get('email', 'port'))
        sever.login(read_config.congfig_read(self.path).get('email', 'SendName'),
                    read_config.congfig_read(self.path).get('email', 'password'))
        sever.sendmail(read_config.congfig_read(self.path).get('email', 'SendName'),
                       ast.literal_eval(read_config.congfig_read(self.path).get('email', 'receive')),  # Pass the string, not a list
                       self.msg.as_string())
        sever.close()
        print('邮件发送成功')





